
#!/bin/sh
#
# Public domain
# Author: emugwaneza
# usage me command_name

echo "$1" > /tmp/wm_binding.fifo
exit 1


mkdir /tmp/manu
cd /tmp/manu


if [ $2 = "stop" ]
 then
  echo "stopping $1 xdotool"
  kill `ps aux|grep xdotool|grep $1 | awk '{print $2}'`
  echo "del icon"
  rm -f leanback.png
  echo "stopping $1"
  xdotool search  --name "$1" key alt+F4
  exit 0
fi

if [ $2 = "start" ]
 then
  echo start $1 with $3
  winid=$(xdotool search --name "$1")
  if [ -n "$winid" ] ; then
	  echo "$1 exists : " $winid
	  xdotool search  --name "$1" windowmove 0 0 windowsize 50% 100% windowmap --sync windowfocus
	  exit 0
  else
	  echo "exec $3"
	  $3 &
	  xdotool search --sync --onlyvisible --name "$1" windowmove 0 0 windowsize 50% 100% windowmap --sync windowfocus
	  winid=$(xdotool search --onlyvisible --name "$1")
	  (xdotool behave $winid focus exec import -windowid %$winid $1.png)&
	  echo "winid: $winid launched"
	  exit 0
  fi
fi





echo "winid: " $winid




#premier screenshot au lancement
import -windowid $winid leanback.png

#si perte changemnt de focus:
#xdotool behave $winid  blur exec import -windowid $winid leanback.png

xdotool search --onlyvisible --name "Youtube - Leanback" behave %@ focus exec sh -c "echo leanback focus %@"



#screenshot all windows
wmctrl -l | awk '{print $1}' | while read id; do import -windowid  $id $id.png;  done










#winid=$(chromium-browser --app="http://youtube.com/leanback" & xdotool search --sync --onlyvisible --name "youtube - Leanback")

#echo "jkjjkjkjkjkjkjk" $winid
#stream_url=$(pluzzdl $1 | grep -o 'mms://.*wmv')
#mimms -cr $stream_url $2

#stream_url=$(youtube-dl -g --cookies ${COOKIE_FILE} $1)
#wget --progress=dot -c -O $2 $stream_url
